//
//  InviteDurationCell.swift
//  todoios
//
//  Created by qianli on 2025/4/23.
//

import Foundation
import SwiftyUserDefaults
import SVGKit


class InviteDurationCell: UITableViewCell {
    var headerView:UIImageView!
    var titleLabel: UILabel!
    var timeLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .clear
        GHL4Rhb91XYBer27()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func GHL4Rhb91XYBer27(){
        let rowHeight = 100.0
        let conentHeight = 80.0
        let iLeft = 15.0
        let rowView = UIView(frame: CGRect(x: iLeft, y: rowHeight/2-conentHeight/2, width: qianli_pm_width-30, height: conentHeight))
        self.contentView.addSubview(rowView)
        rowView.backgroundColor = .systemBackground
        rowView.layer.cornerRadius = 10
        let avatarWidth = 50.0
        headerView = UIImageView(frame: CGRect(x: 15, y: rowView.bounds.height/2-avatarWidth/2, width: avatarWidth, height: avatarWidth))
        rowView.addSubview(headerView)
        headerView.image = "qianli_placeholder".qianli_R_image
        headerView.layer.cornerRadius = avatarWidth/2
        headerView.clipsToBounds = true
        
        titleLabel = UILabel(frame: CGRect(x: 15.0+avatarWidth+10.0, y: headerView.frame.minY, width: rowView.bounds.width-40-avatarWidth, height: avatarWidth/2))
        rowView.addSubview(titleLabel)
        titleLabel.font = .systemFont(ofSize: 16)
        titleLabel.textColor = .label
        
        timeLabel = UILabel(frame: CGRect(x: 15+avatarWidth+10, y: headerView.frame.maxY-avatarWidth/2, width: rowView.bounds.width-40-avatarWidth, height: avatarWidth/2))
        rowView.addSubview(timeLabel)
        timeLabel.font = .systemFont(ofSize: 14)
        timeLabel.textColor = .secondaryLabel
    }
    
    func c612AWlbhGInRMY3(info:InviteDurationModel){
        let userName = info.order_user_name
        self.titleLabel.text = userName
        
        let duration = info.bonus_duration
        timeLabel.text = bBaITeCotrSFXYrr(duration)
        
//        <Avatar src={auth.publicSetting.frontend_url + '/api/public/utils/getAvatar/' + node.order_user_name + '.svg'}
        let frontendUrl = Defaults[\.frontendUrl]
        let avatarUrl = "\(frontendUrl)/api/public/utils/getAvatar/\(userName).svg"
        GXwrjdpULOsWeoiB(avatarUrl)
        
    }
    
    func bBaITeCotrSFXYrr(_ seconds: Int) -> String {
        // 一天有 86400 秒，计算秒对应的天数
        let days = Double(seconds) / 86400
        // 使用 NumberFormatter 来格式化结果，保留两位小数
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        formatter.minimumFractionDigits = 2
        formatter.maximumFractionDigits = 2
        
        
        if let formattedDays = formatter.string(from: NSNumber(value: days)) {
            return "\(LocalizedString("Acquired Time"))\(formattedDays)天"
        }
        return LocalizedString("Unacquired Time")
    }
    
    func GXwrjdpULOsWeoiB(_ avatarUrl:String){
        if let imageUrl = URL(string: avatarUrl){
            if imageUrl.pathExtension.lowercased() == "svg" {
                let task = URLSession.shared.dataTask(with: imageUrl) { [weak self] (data, response, error) in
                    if let error = error {
                        QianliUniversalFunc.QianliLog("Error loading image: \(error)")
                        self?.headerView?.image = "qianli_placeholder".qianli_R_image
                        return
                    }

                    guard let data = data else {
                        QianliUniversalFunc.QianliLog("No data received")
                        self?.headerView.image = "qianli_placeholder".qianli_R_image
                        return
                    }

                    // 在后台线程解析 SVG 数据
                    DispatchQueue.global().async {
                        if let svgImage = SVGKImage(data: data) {
                            let uiImage = svgImage.uiImage
                            // 回到主线程更新 UI
                            DispatchQueue.main.async {
                                self?.headerView.image = uiImage
                            }
                        }
                    }
                }
                // 启动任务
                task.resume()
            }else{
                self.headerView.sd_setImage(with: imageUrl, placeholderImage: nil) { [weak self](image, error, cacheType, url) in
                    if error != nil || image == nil{
                        self?.headerView.image = "qianli_placeholder".qianli_R_image
                    }
                }
            }
        }else{
            self.headerView.image = "qianli_placeholder".qianli_R_image
        }
    }
    
}
